<template>
  <div>
    测试后端API
    <el-input-number v-model="id" placeholder="" @input="sendGetOne"></el-input-number>
    <el-input v-model="aa.name" placeholder="姓名"></el-input>
    <el-input v-model="aa.age" placeholder="年龄"></el-input>
    <el-button type="" @click="sendAdd">添加数据</el-button>
    <el-button type="" @click="sendUpdate">修改数据</el-button>
    <el-button type="" @click="sendDelete">删除数据</el-button>
    <el-button type="" @click="sendGetOne">获取数据</el-button>
    <el-button type="" @click="sendGetAll">获取全部数据</el-button>
    <br>
    启用查询：<el-switch v-model="useQuery"></el-switch><br>
    <el-pagination
      background
      layout="prev, pager, next"
      v-model:currentPage="pager.pagerIndex"
      :page-size="pager.pagerSize"
      :total="pager.pagerTotal">
    </el-pagination>
    <div v-for="(item, index) in dataList" :key="index">
      {{item}}
    </div>
    <el-button type="" @click="find(100)">查找</el-button>
    {{array}}
  </div>
</template>

<script>
// 在指定范围内筛选素数
const find1 = (endNumber) => {
  const arr = [2, 3, 5, 7, 11]
  // 初始化
  for (let i = 13; i < endNumber; i += 2) {
    arr.push(i)
  }
  const kaifang = parseInt(Math.sqrt(endNumber))
  // 去掉素数的倍数
  for (let i = 1; i < arr.length; i += 1) {
    if (arr[i] < kaifang) {
      // 获取基数
      const a = arr[i] // 3 开始，去掉其倍数
      let cpp = a + a // 6 倍数
      for (let j = i + 1; j < arr.length; j += 1) {
        if (arr[j] === cpp) {
          // 删掉
          arr.splice(j, 1)
          cpp = cpp + a // 倍数
        }
        if (cpp < arr[j]) cpp = cpp + a // 倍数
      }
    }
  }
  return arr
}

// 猜数字 nAmB 的形式

// 建立初始数组
// 判断函数
// 筛选函数

const cszInit = () => {
  const arr = []
  for (let a = 0; a < 10; a++) {
    for (let b = 0; b < 10; b++) {
      if (b !== a) {
        for (let c = 0; c < 10; c++) {
          if (c !== b && c !== a) {
            for (let d = 0; d < 10; d++) {
              if (d !== c && d !== b && d !== a) {
                arr.push(a.toString() + b.toString() + c.toString() + d.toString())
              }
            }
          }
        }
      }
    }
  }
  return arr
}

const cszCheck = (str1, str2) => {
  const s1 = [...str1]
  const s2 = [...str2]
  let a = 0
  let b = 0
  for (let i = 0; i < 4; i++) {
    for (let j = 0; j < 4; j++) {
      if (s1[j] === s2[i]) {
        if (i === j) a++
        else b++
      }
    }
  }
  return { a, b }
}

// 开始游戏
function setup1 () {
  // 初始化
  const arr = cszInit()
  // 验证
  const { a, b } = cszCheck('0123', '3152')
  console.log(a, b)
  // 筛选
}

setup1()

</script>

<script setup>
import { reactive, ref, watch } from 'vue'
import axios from 'axios'
// import func from '../../vue-temp/vue-editor-bridge'

// 记录ID
const id = ref(98)

const array = ref([])
const find = (n) => {
  array.value = find1(parseInt(id.value))
}

// 是否使用查询条件
const useQuery = ref(false)

// 分页信息
const pager = reactive({
  pagerIndex: 1,
  pagerSize: 20,
  pagerTotal: 100
})

// model
const aa = reactive({
  name: 'jyk',
  age: 18
})

// 数据列表
const dataList = reactive([])

// 添加
const sendAdd = () => {
  axios.post('/api/100/10', aa).then((re) => {
    console.log('添加完成，新ID：', re)
  })
}

// 修改
const sendUpdate = () => {
  axios.post(`/api/100/20/${id.value}`, aa).then((re) => {
    console.log('修改完成，影响行数：', re)
  })
}
// 删除
const sendDelete = () => {
  axios.get(`/api/100/30/${id.value}`).then((re) => {
    console.log('删除完成，影响行数：', re)
  })
}
// 获取一条记录
const sendGetOne = () => {
  axios.get(`/api/100/40/${id.value}`).then((re) => {
    console.log('获取记录：', re)
    Object.assign(aa, re.data.model[0])
  })
}
// 获取全部数据
const sendGetAll = () => {
  const query = {}
  if (useQuery.value) {
    if (aa.name.length > 0) query.name = [403, aa.name]
    if (aa.age.length > 0) query.age = [415, aa.age]
  }
  axios.post(`/api/100/60/${id.value}`, query).then((re) => {
    console.log('获取记录：', re)
    dataList.length = 0
    dataList.push(...re.data.list)
  })
}

// 分页
watch(() => pager.pagerIndex, (v) => {
  console.log(v)
  const info = {
    pager: pager,
    query: {},
    useCount: false
  }
  if (v === 1) info.useCount = true

  if (useQuery.value) {
    if (aa.name.length > 0) info.query.name = [403, aa.name]
    if (aa.age.length > 0) info.query.age = [415, aa.age]
  }
  axios.post('/api/100/50', info).then((re) => {
    console.log('获取记录：', re)
    // 设置总记录数
    pager.pagerTotal = re.data.pager.pagerTotal
    pager.pagerSize = re.data.pager.pagerSize
    dataList.length = 0
    dataList.push(...re.data.list)
  })
})

</script>
